import shutil

from jinja2 import Environment, FileSystemLoader

from up import Everything
from msg.uat_remote_control import nvr
from common.tools import Tool


def gen_data():
    data = []
    start = 1
    for k, l in nvr.items():
        for c in l:
            ssrc = Tool().from_camId_chanId_to_ssrc(k, c)
            title = f"录像机:{k} 通道{c} 流标记{ssrc}"
            data.append({"id": f"cam_{start}", "title": title, "ssrc": ssrc})
            start = start + 1
    return data


def gen(data, osname):
    host = "http://localhost:8000" if osname == "win" else "http://39.106.152.15/deploy/static/gb"
    env = Environment(loader=FileSystemLoader('./'))
    template = env.get_template('index_template.html')
    dic = {"li": data, "host": host}
    output_from_parsed_template = template.render(data=dic)
    print(output_from_parsed_template)

    path = "./index.html"
    # to save the results

    if osname == "win":
        with open(path, "w") as fh:
            fh.write(output_from_parsed_template)
        shutil.copyfile(path, "D://mygo/gb28181/test/index.html")
    else:
        with open(path, "w", encoding="utf-8") as fh:
            fh.write(output_from_parsed_template)
        Everything().copy_html()


if __name__ == '__main__':
    d = gen_data()
    gen(d, "linux")
    # gen(d, "win")
